package com.tarena.lbs.message.web.service.impl;

import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.tarena.lbs.basic.api.UserGroupApi;
import com.tarena.lbs.common.message.constance.Const;
import com.tarena.lbs.message.web.mapper.MsgMapper;
import com.tarena.lbs.message.web.mapper.TriggerMsgTemplateMapper;
import com.tarena.lbs.message.web.service.TriggerMessageService;
import com.tarena.lbs.pojo.message.dos.Msg;
import com.tarena.lbs.pojo.message.dos.TriggerMsgTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * 触发消息服务实现类
 */
@Service
public class TriggerMessageServiceImpl implements TriggerMessageService {
    @Autowired
    TriggerMsgTemplateMapper triggerMsgTemplateMapper;

    @Autowired
    MsgMapper msgMapper;

    @Autowired
    private UserGroupApi userGroupApi;

    /**
     * 处理触发消息
     * @param jsonStr 消息数据
     * @throws Exception
     */
    @Override
    public void handleMessage(String jsonStr) throws Exception {
        //从消息队列中接收json
        // 创建一个ObjectMapper对象
        ObjectMapper mapper = new ObjectMapper();
        // 解析JSON字符串
        JsonNode jsonNode = mapper.readTree(jsonStr);

        //获取用户编号，有了用户编号才能推送出去
        int userId = jsonNode.get("userId").asInt();

        Integer activityId = jsonNode.get("activityId") == null ? 0 : jsonNode.get("activityId").asInt();

        // 获取userAction字段的值
        String userAction = jsonNode.get("userAction") == null ? "" : jsonNode.get("userAction").asText();
        String articleTitle = jsonNode.get("articleTitle") == null ? "" : jsonNode.get("articleTitle").asText();
        String articleId = jsonNode.get("articleId") == null ? "" : jsonNode.get("articleId").asText();
        String userName = jsonNode.get("username") == null ? "" : jsonNode.get("username").asText();
        //判断触发消息类型



        //根据消息类型到模板表查找消息模板
        TriggerMsgTemplate triggerMsgTemplate = triggerMsgTemplateMapper.selectByUserAction(userAction);


        //获取消息标题
        String msgTitle = triggerMsgTemplate.getMsgTitle();
        //获取消息内容
        String content = triggerMsgTemplate.getContent();

        if (content.contains(Const.USER_NAME) || msgTitle.contains(Const.USER_NAME) || content.contains(Const.ARTICLE_TITLE)) {
            content = content.replace(Const.USER_NAME,userName);
            content = content.replace(Const.LIKED_USER_NAME,userName);
            content = content.replace(Const.ARTICLE_TITLE,articleTitle);
            msgTitle = msgTitle.replace(Const.USER_NAME,userName);
            msgTitle = msgTitle.replace(Const.ARTICLE_TITLE,articleTitle);
        }
        //把消息保存数据库
        Msg msg = new Msg();
        msg.setUserId(userId);
        msg.setMsgTitle(msgTitle);
        msg.setContent(content);
        msg.setStatus(1);
        msg.setActivityId(activityId);
        msg.setTemplateId(triggerMsgTemplate.getId());
        int row = msgMapper.insert(msg);

        /*//发送推送消息
        WebSocketServer.sendLbsInfo(userId);*/

    }


}
